package com.zhangxun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhangxun.domain.Order;
import com.zhangxun.service.OrderService;
import com.zhangxun.vo.OrderConfirm;
import com.zhangxun.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "订单接口管理")
@RestController
@RequestMapping("/p/myOrder")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @PostMapping("/confirm")
    @ApiOperation("购物车结算接口")
    public ResponseEntity<OrderVo> confirm(@RequestBody OrderConfirm orderConfirm){

        OrderVo orderVo = orderService.confirm(orderConfirm);

        return ResponseEntity.ok(orderVo);
    }


    @PostMapping("/submit")
    @ApiOperation("提交订单接口")
    public ResponseEntity<String> submit(OrderVo orderVo){

        String orderNum = orderService.submitOrder(orderVo);

        return ResponseEntity.ok("aaa:"+orderNum);
    }


    /**
     * 远程调用通过订单号获取订单信息
     * @param orderNo
     * @return
     */
    @GetMapping("/loadById/{orderNo}")
    public Order loadById(@PathVariable("orderNo") String orderNo){

        return orderService.getById(orderNo);
    }


    /**
     * 远程调用通过订单号修改订单状态
     * @param orderNo
     */
    @PostMapping("/updateOrderStatus")
    void updateOrderStatus(@RequestBody String orderNo){
        orderService.update(new LambdaUpdateWrapper<Order>()
                .set(Order::getStatus,2)
                .set(Order::getIsPayed,1)
                .set(Order::getPayTime,new Date())
                .eq(Order::getStatus,1)
                .eq(Order::getIsPayed,0)
                .eq(Order::getOrderNumber,orderNo));
    }
}
